package com.wryyyyy.controller;

import cn.hutool.core.map.MapUtil;
import com.wryyyyy.dto.Result;
import com.wryyyyy.service.IApproachService;
import com.wryyyyy.utils.FileLoad;
import com.wryyyyy.utils.MyArrayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.yaml.snakeyaml.util.ArrayUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

import static com.wryyyyy.utils.SystemConstants.FILE_ABSOLUTE_PATH;
import static com.wryyyyy.utils.SystemConstants.FILE_UPLOAD_PATH;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wryyyyy
 * @since 2023-11-12
 */
@Slf4j
@RestController
@RequestMapping("/approach")
public class ApproachController {
    @Resource
    private IApproachService approachService;

    @Autowired
    private FileLoad fileLoad;

    /**
     * 上传 Excel文件 和 期望的分析方法
     * @return 从 Excel文件提取的数据 和 分析结果
     */
    @PostMapping("/upload")
    public Result upload(
            @RequestParam("file") MultipartFile file
//            @RequestParam(value = "method", defaultValue = "1") Integer method
    ) {
        String filePath = fileLoad.uploadOneFile(FILE_UPLOAD_PATH, file);
        if (filePath == null) {
            return Result.fail("上传失败");
        }
        return approachService.upload(FILE_ABSOLUTE_PATH + FILE_UPLOAD_PATH + filePath);
    }

    @PostMapping("/analysis/dengshi")
    public Result dengshi(
            @RequestBody Map<String, Object> params
    ) {
        String[] arguments = MapUtil.get(params, "arguments", String[].class);
        return Result.ok(approachService.deal(arguments, 1));
    }

    @PostMapping("/analysis/huidu")
    public Result huidu(
            @RequestBody Map<String, Object> params
    ) {
        String[] arguments = MapUtil.get(params, "arguments", String[].class);
        String[] qs = MapUtil.get(params, "qs", String[].class);
        String[] args = MyArrayUtil.concat(arguments, qs);

        return Result.ok(approachService.deal(args, 2));
    }

    @PostMapping("/analysis/three")
    public Result three(
            @RequestBody Map<String, Object> params
    ) {
        String[] arguments = MapUtil.get(params, "arguments", String[].class);
        String[] qs = MapUtil.get(params, "qs", String[].class);
        String[] ps = MapUtil.get(params, "ps", String[].class);
        String[] args = MyArrayUtil.concat(arguments, ps, qs);

        return Result.ok(approachService.deal(args, 3));
    }
}